package com.back.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.base.dao.BaseDao;
import com.base.dao.IBaseDao;
import com.domain.ActivityTable;
import com.util.comm.DateTimeUtil;

public class ActivityTableServiceImpl implements  IActivityTableService {
	private static final Logger LOGGER = Logger.getLogger(ActivityTableServiceImpl.class);
	private  IBaseDao<ActivityTable, Long>   dao;
	public  ActivityTableServiceImpl(){
		dao =  new BaseDao<ActivityTable, Long>(ActivityTable.class);
	}
	 public  boolean  saveOrUpdateActi(ActivityTable  table){
		  try{
			  dao.saveOrUpdate(table);
		  }catch (Exception e) {
			  LOGGER.error("ActivityTable  saveOrUpdate error", e);
			  return false;
		}
		  return true;
	 }
	public ActivityTable findActivityById(String id) {
		try{
			String  str="  SELECT * FROM  actitytable WHERE  id=? ";
		    List<ActivityTable>	list = dao.findList(str, new Object[]{id});
		    if(list!=null  &&  !list.isEmpty())
		    {
		    	return list.get(0);
		    }
		}catch (Exception e) {
			LOGGER.error(" ActivityTable  findActivityById "+id +" error", e);
		    return null;
	}
		return null;
	}
	public   List<ActivityTable>  findByMap(Map<String,String> parameMap){
		try{
			String  str="  SELECT * FROM  actitytable WHERE  type=?  ";
			if(parameMap.get("page") != null  &&  parameMap.get("limit") !=null )
			{
				 int start=Integer.parseInt(parameMap.get("page"))-1;
				 int limit =Integer.parseInt(parameMap.get("rows"))-1;
				 str +=" limit "+start*limit+","+limit ;
			}
		   List<ActivityTable>  list = dao.findList(str,  new Object[]{parameMap.get("type")});
		   if (list !=null  && !list.isEmpty())
		   {
			   List<ActivityTable> newlist =  new ArrayList<ActivityTable>();
			   for(ActivityTable activityTable : list)
			   {
				   activityTable.setCreatTimeStr
				   (DateTimeUtil.switchDateToString(activityTable.getCreatTime(),"yyyy-MM-dd HH:mm:ss"));
				   newlist.add(activityTable);
			   }
			   return  newlist;
		   }
		   
		}catch (Exception e) {
			LOGGER.error(" ActivityTable  findByMap error", e);
		    return null;
	  }
		return null;
	}
	public   int count (Map<String,String> parameMap){
		try{
			String  str="  SELECT * FROM  actitytable WHERE  type=?  ";
		    List<ActivityTable> list= dao.findList(str,  new Object[]{parameMap.get("type")});
		    if(list!=null )
		    {
		    	return list.size();
		    }
		}catch (Exception e) {
			LOGGER.error(" ActivityTable  count error", e);
		    return 0;
	}
		return 0;
	}
	 public   boolean   deleteByIdAndType(String ids,String type){
		 boolean isFlag = true;
			if ("".equals(ids))
				return false;
			String sql = "";
			String[] delteid = ids.split("_");
			try {
				for (int i = 0; i < delteid.length; i++) {
					sql = "delete  from actitytable where  id=" + delteid[i] +"  and  type="+type;		
					dao.delete(sql);
				}
			} catch (Exception e) {
				e.printStackTrace(); 
				LOGGER.error(" ActivityTable  deleteByIdAndType error", e);
				return false;
			}
			return isFlag;
	 }
	 public ActivityTable findActivityByType(Map<String,String> parameMap) {
			String str = " SELECT * FROM actitytable WHERE type =? ";
			if( !"".equals(parameMap.get("lanType"))  )
			{
				str  += " and  lanType = "+ parameMap.get("lanType");
			}
			try{
				List<ActivityTable>	list = dao.findList(str, new Object[]{parameMap.get("type")});
				  if(list!=null  &&  !list.isEmpty())
				    {
				    	return list.get(0);
				    }
			}catch(Exception e){
				LOGGER.error(" ActivityTable  findActivityById "+parameMap.get("type") +" error", e);
			    return null;
			}
			
			return null;
		}
	  public   List<ActivityTable>  findByActivityList(Map<String,String> parameMap){
		  try{
				String  str="  SELECT * FROM  actitytable WHERE  1=1   ";
				if(null!=parameMap.get("type"))
				{
					str+=" and type= "+parameMap.get("type");
				}
				if(null!=parameMap.get("lanType"))
				{
					str+=" and lanType= "+parameMap.get("lanType");
				}
				if(null!=parameMap.get("id"))
				{
                   str+=" and id= " + parameMap.get("id");				
				}
				str+="  ORDER  BY creatTime DESC ";
				if(parameMap.get("page") != null  &&  parameMap.get("limit") !=null )
				{
					 int start=Integer.parseInt(parameMap.get("page"))-1;
					 int limit =Integer.parseInt(parameMap.get("limit"));
					 str +=" limit "+start*limit+","+limit ;
				}
			   List<ActivityTable>  list = dao.findList(str);
			   return list;
			   
			}catch (Exception e) {
				LOGGER.error(" ActivityTable  findByMap error", e);
			    return null;
		  }
			
	  }
}
